

using System.Numerics;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_Max_RT : RT_Node
    {
        public RT_SingleValuePort[] Values_RT;

        public RT_SingleValuePort Max_RT;

        public Node_Max_RT(Node_Max sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            Values_RT = BuildRTPortArrayFromGroup<RT_SingleValuePort>(EPortIOType.Input, sourceData.Values);

            Max_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Max, this);
            Max_RT.OnGetValue = GetMax;

        }

        public AValue GetMax(DAG_Executor executor)
        {
            Max_RT.SourceValue.Reset();
            if (Values_RT.Length > 0)
            {
                var valueType = Max_RT.SourceValue.ValueType;
                var first = Values_RT[0].GetValue(executor);
                if (valueType == AValue.EValueType.Integer)
                {
                    int maxInt = first.ToInteger().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        int currInt = Values_RT[i].GetValue(executor).ToInteger().Item2;
                        if (maxInt < currInt)
                        {
                            maxInt = currInt;
                        }
                    }
                    Max_RT.SourceValue.SetInteger(maxInt);
                }
                else if (valueType == AValue.EValueType.Scalar)
                {
                    float maxfloat = first.ToScalar().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        float currFloat = Values_RT[i].GetValue(executor).ToScalar().Item2;
                        if (maxfloat < currFloat)
                        {
                            maxfloat = currFloat;
                        }
                    }
                    Max_RT.SourceValue.SetScalar(maxfloat);
                }
                else if (valueType == AValue.EValueType.Vector2)
                {
                    Vector2 maxVec2 = first.ToVector2().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        Vector2 currVec2 = Values_RT[i].GetValue(executor).ToVector2().Item2;
                        maxVec2 = Vector2.Max(maxVec2, currVec2);
                    }
                    Max_RT.SourceValue.SetVector2(maxVec2);
                }
                else if (valueType == AValue.EValueType.Vector3)
                {
                    Vector3 maxVec3 = first.ToVector3().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        Vector3 currVec3 = Values_RT[i].GetValue(executor).ToVector3().Item2;
                        maxVec3 = Vector3.Max(maxVec3, currVec3);
                    }
                    Max_RT.SourceValue.SetVector3(maxVec3);
                }
            }

            return Max_RT.SourceValue;
        }
    }
}